package com.aw.baselib.widgets.font

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.TextUtils
import android.util.AttributeSet
import android.widget.EditText
import com.aw.baselib.R
import com.aw.baselib.base.AwBaseApplication
import com.aw.baselib.util.AwDisplayUtil
import com.aw.baselib.util.AwToastUtil

/**
 * @author : hzw
 * https://blog.csdn.net/yuanxw44/article/details/80019501
 */
@SuppressLint("AppCompatCustomView")
abstract class AwEtRegularBase(private val mContext: Context, attrs: AttributeSet?) : EditText(
    mContext, attrs
) {
    private var defaultLength = 20
    private var isSingleLine = true
    protected abstract fun setViewBg()

    init {
        val a = mContext.obtainStyledAttributes(attrs, R.styleable.AwEtAttrs)
        defaultLength = a.getInteger(R.styleable.AwEtAttrs_maxInputLength, 20)
        isSingleLine = a.getBoolean(R.styleable.AwEtAttrs_isSingleLine, true)
        setHintTextColor(resources.getColor(R.color.color_app_gray_light))
        setTextColor(resources.getColor(R.color.color_app_blue_night))
        textSize = 14f
        if (isSingleLine) {
            setSingleLine()
            ellipsize = TextUtils.TruncateAt.END
            //            setHeight(AwDisplayUtil.dip2px(context, 45));
        } else {
//            setMinHeight(AwDisplayUtil.dip2px(context, 45));
            setPadding(
                AwDisplayUtil.dip2px(mContext, 14f), AwDisplayUtil.dip2px(
                    mContext, 5f
                ), AwDisplayUtil.dip2px(mContext, 14f), AwDisplayUtil.dip2px(
                    mContext, 5f
                )
            )
        }
        filters = arrayOf<InputFilter>(LengthFilter(defaultLength))
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun getMaxLines(): Int {
        return 1
    }

    override fun isSingleLine(): Boolean {
        return true
    }

    fun showEmptyEmailHint() {
        showCustomHint("请输入邮箱")
    }

    fun showEmptyPwdHint() {
        showCustomHint("请输入密码")
    }

    fun showEmptyPhoneHint() {
        showCustomHint("请输入手机号")
    }

    fun showEmptyCommonHint() {
        showCustomHint("请输入内容")
    }

    fun showFormatHint() {
        showCustomHint("异常! 格式错误")
    }

    fun showFormatPwdHint() {
//        showCustomHint("Please enter 6-20 characters, using uppercase and lowercase letters and numbers.");
        showCustomHint("异常! 格式错误")
    }

    fun showInvalidCodeHint() {
        showCustomHint("验证码无效，请重新输入")
    }

    fun showDifferentPwdHint() {
        showCustomHint("两次密码不一致，请重新输入")
    }

    fun showSamePwdHint() {
        showCustomHint("新密码不能与旧密码相同")
    }

    fun showDifferentEmailHint() {
        showCustomHint("邮箱验证不一致")
    }

    fun showCustomHint(msg: String?) {
        setViewBg()
        AwToastUtil.showShort(AwBaseApplication.instance, msg)
    }
}